package com.lucas_main.uvicultimate;

import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	public static final String KEY_ROWID  = "rowid";
	public static final String KEY_ID  = "id";
	public static final String KEY_USERNAME  = "username";
	public static final String KEY_FULL_NAME  = "full_name";
	public static final String KEY_EMAIL  = "email";
	public static final String KEY_STATUS  = "status";
	public static final String KEY_PHONE  = "phone";
	private static final String TAG = "DBAdapter";
	private static final String DATABASE_TABLE = "";
	
	private static final String DATABASE_NAME = "UserDB.db";
	
	private static final int DATABASE_VERSION = 1;
	
	
		
	private final Context context;
	
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	
	public DBAdapter(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
		
	}
	
	public static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper (Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);			
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			try {
				//Create user info
				db.execSQL("CREATE TABLE IF NOT EXISTS UserInfo (id INT, username VARCHAR, "
						+ "full_name VARCHAR, email VARCHAR, status INT, phone VARCHAR);");
				//Create Thread Table
				db.execSQL("CREATE TABLE IF NOT EXISTS ForumThreads (id INT, author_names VARCHAR, "
						+ "last_update INT, subject VARCHAR, time_posted INT, status INT, last_msg_id INT, "
						+ "num_replies INT, body TEXT, gender VARCHAR);");
				
			}
			catch (SQLException e){
				e.printStackTrace();
				Log.e("SQL Error", e.toString());
			}
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS contacts");
		onCreate(db);
		 }
	}
	
	//---opens the database---
	public DBAdapter open() throws SQLException
	{
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	//---closes the database---
	public void close()
	{
		DBHelper.close();
	}

	//---update user info table---
	public long updateUserInfo(String id, String username, String full_name, String email, int status, String phone)
	{
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_ID, id);
		initialValues.put(KEY_USERNAME, username);
		initialValues.put(KEY_FULL_NAME, full_name);
		initialValues.put(KEY_EMAIL, email);
		initialValues.put(KEY_STATUS, status);
		initialValues.put(KEY_PHONE, phone);
		return db.insert("UserInfo", null, initialValues);
		
	}
	
	public long insertThread(int id, String authorNames, int lastUpdate, String subject, 
								int timePosted, int status, int lastMessageId, int numberOfReplies, String body,
								String gender) throws SQLException{
		
		db.execSQL("CREATE TABLE IF NOT EXISTS ForumThreads (id INT, author_names VARCHAR, "
				+ "last_update INT, subject VARCHAR, time_posted INT, status INT, last_msg_id INT, "
				+ "num_replies INT, body TEXT, gender VARCHAR);");
		ContentValues vals = new ContentValues();
		vals.put("id", id);
		vals.put("author_names", authorNames);
		vals.put("last_update", lastUpdate);
		vals.put("subject", subject);
		vals.put("time_posted", timePosted);
		vals.put("status", status);
		vals.put("last_msg_id", lastMessageId);
		vals.put("num_replies", numberOfReplies);
		vals.put("body", body);
		vals.put("gender", gender);
		
		return db.insert("ForumThreads", null, vals);
	}
	
	public int getUserGender() throws SQLException {
		
		Cursor mCursor = db.query("UserInfo", new String [] {"status"}, null, null, null, null, null);
		mCursor.moveToLast();
		
		int i = mCursor.getInt(0);
		Log.d("SQL Transaction", "Return status = " + i);
		return i;
	}
	
	public Cursor getThreads() throws SQLException {
		return db.query("ForumThreads", new String [] {"subject"},null, null,null,null,null);
	}
	
	//---retrieve particular record---
	public Cursor getUser() throws SQLException {
		Cursor mCursor = db.query("UserInfo", new String[] {"id", "username", "full_name", "email", "status", "phone"} , null, null, null, null, null);
		return mCursor;
	}
	
	
	
	
}
